﻿<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>成本发票登记</title>
    <link rel="stylesheet" type="text/css" href="../Content/Css/OperaMasksUI/default/om-all.css" />
    <link rel="stylesheet" type="text/css" href="../Content/Css/list.css" />
    <script type="text/javascript" src="../Scripts/jquery.1.6.3.min.js"></script>
    <script type="text/javascript" src="../Scripts/json2.js"></script>
    <script type="text/javascript" src="../Scripts/common.js"></script>
    <script type="text/javascript" src="../Scripts/OperaMasksUI/operamasks-ui.min.js"></script>
    <script type="text/javascript" src="../Scripts/Plugin/jquery.form.js"></script>
    <script type="text/javascript" src="../Scripts/OperaMasksUI/Plugin/om-yj-container.js"></script>
    <script type="text/javascript" src="../Scripts/Plugin/artDialog4.1.7/jquery.artDialog.js?skin=default"></script>
    <script type="text/javascript" src="../Scripts/Plugin/artDialog4.1.7/plugins/iframeTools.js"></script>
    <script type="text/javascript" src="../Scripts/banBackSpace.js"></script>
    <script type="text/javascript">
        var validatorForm, submitForm, detailForm, detailPanel, showDetialForm;
        var actionRoot = "../handler/ajax.ashx?type=AjaxSystem", comboxRateData = [];

        $(document).ready(function () {
            currentUID = art.dialog.data('currentUID');
            rowData = JSON2.parse(art.dialog.data('RowData'));
            if (rowData.length == 1) {
                detailForm = $('#viewForm');
                $.ajax({
                    cache: false, async: false,
                    url: '../Handler/combox.ashx?view=vBaseData',
                    dataType: 'json', type: 'post',
                    beforeSend: function () {
                        $.omMessageBox.waiting({
                            title: '请等待',
                            content: '服务器正在加载相关基础数据信息,请稍后...'
                        });
                    },
                    complete: function () {
                        $.omMessageBox.waiting("close");
                    },
                    success: function (data) {
                        comboxRateData = [];
                        $.each(data, function (index, value) {
                            switch (value.Type) {
                                case '发票税率':
                                    comboxRateData.push({ name: value.Name, code: value.Code, type: value.Type });
                                    return true;
                                default:
                                    return true;
                            }
                        });
                        $('#TaxRate', detailForm).omCombo({
                            dataSource: comboxRateData,
                            valueField: 'name',
                            optionField: 'name',
                            inputField: 'name',
                            onValueChange: function (target, newValue, oldValue, event) {
                                $("#Amount2").val($('#Amount').val() - parseFloat($('#Amount').val() / (1 + newValue)).toFixed(2));
                            }
                        });
                    },
                    error: function (result) {
                        $.omMessageBox.alert({
                            content: '发现系统错误,请重新登录<BR>错误码：' + result.status
                        });
                    }
                });
                validatorForm = $('#viewForm').validate({
                    errorContainer: "#errorMessages",
                    errorLabelContainer: "#errorMessages",
                    wrapper: "span",
                    errorClass: "omError",
                    rules: {
                        CostNO: { required: true },
                        TaxRate: { required: true,number:true }
                    },
                    messages: {
                        CostNO: { required: "[发票号码]不能为空" },
                        TaxRate: { required: "[发票税率]不能为空", number: "[发票税率]必须是数字" }
                    }
                });
                $("#Amount").attr("disabled",true);
                showDetialForm("发票登记信息", rowData[0]);
                $('span#button-save', detailForm).omButton({
                    icons: { left: '../Content/Images/List/accept.png' }, width: 80,
                    onClick: function () {
                        if (validatorForm.form()) {
                            if ($("#TaxRate").val() > 0 && $("#TaxRate").val() < 1) {
                                detailForm.attr("action", actionRoot + "&method=UpdateCostNo");
                                submitForm(detailForm, function (data) {
                                    if (data.IsError) {
                                        $.omMessageBox.alert({
                                            type: 'error',
                                            content: '错误<BR>：' + data.Message,
                                            onClose: function () {
                                                $("#CostNO", detailForm).focus();
                                            }
                                        });
                                    }
                                    else {
                                        $.omMessageBox.alert({
                                            type: 'success',
                                            content: '成本发票登记成功',
                                            onClose: function () {
                                                art.dialog.close();
                                            }
                                        })
                                    }
                                });
                            }
                        }
                    }
                });
                $('span#button-cancel', detailForm).omButton({
                    icons: { left: '../Content/Images/List/cross.png' }, width: 80,
                    onClick: function () {
                        art.dialog.close();
                    }
                });
            }
        });

        showDetialForm = function (title, rowData) {
            if (detailPanel) {
                $("input[type=text]", detailPanel).val("");
                detailPanel.omPanel("open");
            }
            else {
                detailPanel = $('#detail-form').omPanel({
                    width: 'fit',
                    title: "<span style='color:#15428B;font-weight: bold;padding:6px;'>" + title + "</span>",
                    collapsed: false,
                    collapsible: false,
                    closable: false
                });
            }
            validatorForm.resetForm();
            rowData = rowData || {};
            $.each(rowData, function (n, value) {
                if (n == 'TaxRate') {
                    $('#' + n, detailForm).omCombo("value", value);
                }
                else {
                    $("input[id='" + n + "']", detailPanel).val(value);
                }
            })
        };

        submitForm = function (mainform, success, error) {
            if (!mainform)
                return false;
            else {
                mainform.ajaxSubmit({
                    type: "POST",
                    dataType: 'json',
                    success: success,
                    clearForm: false,
                    beforeSubmit: function (formData, jqForm, options) {
                        $(":checkbox,:radio", jqForm).each(function () {
                            if (!existInFormData(formData, this.name)) {
                                formData.push({ name: this.name, type: this.type, value: this.checked });
                            }
                        });
                        for (var i = 0, l = formData.length; i < l; i++) {
                            var o = formData[i];
                            if (o.type == "checkbox" || o.type == "radio") {
                                o.value = $("[name=" + o.name + "]", jqForm)[0].checked ? "true" : "false";
                            }
                        }
                    },
                    beforeSend: function (a, b, c) {
                        $.omMessageBox.waiting({
                            title: '正在保存数据中...',
                            content: '服务器正在处理请求......'
                        });
                    },
                    complete: function () {
                        $.omMessageBox.waiting("close")
                    },
                    error: function (result) {
                        $.omMessageBox.waiting("close")
                        $.omMessageBox.alert({
                            content: '错误 <BR>：' + result.status
                        });
                    }
                });
            }
        }

        function existInFormData(formData, name) {
            for (var i = 0, l = formData.length; i < l; i++) {
                var o = formData[i];
                if (o.name == name) return true;
            }
            return false;
        }

    </script>
</head>
<body>
    <div id="detail-form">
        <form id="viewForm">
            <div id="errorMessages" class="error"></div>
            <table style="width: 100%;">
                <tr>
                    <td align="left" style="display:none;" colspan="3">
                        <input type="text" class="input-text" name="UID" id="UID" /></td>
                </tr>
                <tr>
                    <td align="right" style="width: 80px; color: red;">发票号码</td>
                    <td align="left" style="width: 150px">
                        <input type="text" class="input-text" name="CostNO" id="CostNO" /></td>
                    <td align="center" style="width: 80px;"><span id="button-save">保存</span></td>
                </tr>
                <tr>
                    <td align="right" style="width: 80px; margin-left: 10px; color: red;">发票税率</td>
                    <td align="left" style="width: 150px">
                        <input type="text" class="input-text" name="TaxRate" id="TaxRate" />
                    </td>
                    <td align="center" style="width: 80px;"><span id="button-cancel">取消</span></td>
                </tr>
                <tr>
                    <td align="right" style="width: 80px; margin-left: 10px; color: red;">发票金额</td>
                    <td align="left" style="width: 150px">
                        <input type="text" class="input-text" name="Amount" id="Amount" /></td>
                    <td align="right" style="width: 50px;"></td>
                </tr>
                <tr>
                    <td align="right" style="width: 80px; color: red;">无税金额</td>
                    <td align="left" style="width: 150px">
                        <input type="text" class="input-text" name="Amount2" id="Amount2" /></td>
                    <td align="right" style="width: 50px;"></td>
                </tr>
                <tr>
                    <td align="right" style="width: 80px; margin-left: 10px;">备注</td>
                    <td align="left" style="width: 150px">
                        <input type="text" class="input-text" name="Note" id="Note" />
                    </td>
                    <td align="right" style="width: 50px;"></td>
                </tr>
            </table>
        </form>
    </div>
</body>
</html>
